<?php

/**
 * 服务接口
 * @desc
 */
class Api_Service extends PhalApi_Api {

    public function getRules() {
        return [
            'id'         => [
                'id' => ['name' => 'id', 'type' => 'string', 'require' => true, 'desc' => '身份证号码'],
            ],
            'ip'         => [
                'ip' => ['name' => 'ip', 'type' => 'string', 'require' => true, 'desc' => 'ip地址'],
            ],
            'aqi'        => [
                'city' => ['name' => 'city', 'type' => 'string', 'require' => true, 'desc' => '城市名称,中文或全拼'],
            ],
            'weather'    => [
                'city' => ['name' => 'city', 'type' => 'string', 'require' => true, 'desc' => '城市名称,中文或全拼'],
            ],
            'phone'      => [
                'phone' => ['name' => 'phone', 'type' => 'string', 'require' => true, 'desc' => '手机号码,多个用,隔开'],
            ],
            'robot'      => [
                'info' => ['name' => 'info', 'type' => 'string', 'require' => true, 'desc' => '文字信息'],
            ],
            'historyDay' => [
                'month' => ['name' => 'month', 'type' => 'int', 'min' => 1, 'max' => 12, 'require' => true, 'desc' => '月'],
                'day'   => ['name' => 'day', 'type' => 'int', 'min' => 1, 'max' => 31, 'require' => true, 'desc' => '日'],
                'num'   => ['name' => 'num', 'type' => 'int', 'min' => 1, 'max' => 100, 'default' => 10, 'require' => false, 'desc' => '获取多少条'],
            ],
            'shorturl'   => [
                'url' => ['name' => 'url', 'type' => 'string', 'require' => true, 'desc' => '需要转换的长链接（urlencode）']
            ],
            'sendMail'   => [
                'receiver' => ['name' => 'receiver', 'type' => 'email', 'require' => true, 'desc' => 'email 接收者邮箱地址'],
                'title'    => ['name' => 'title', 'type' => 'string', 'require' => true, 'desc' => '邮件标题'],
                'content'  => ['name' => 'content', 'type' => 'string', 'require' => true, 'desc' => '邮件内容']
            ]
        ];
    }

    /**
     * 发送短信
     * @return mixed
     * @throws PhalApi_Exception
     * @throws PhalApi_Exception_InternalServerError
     */
//    public function  sms() {
//
//        $data= DI()->SMS->sendTemplateSMS("18868424455", "asdda", 0);
//        if(!$data){
//            throw new PhalApi_Exception_InternalServerError("系统错误");
//        }
//
//        if($data->statusCode!='000000'){
//            throw new PhalApi_Exception($data->statusMsg,intval($data->statusCode));
//        }
//        return $data;
//    }

    /**
     * 短网址生成
     * @desc
     * @return string object_type
     * @return boolean result 短链的可用状态，true：可用、false：不可用
     * @return string url_short 短链接
     * @return string object_id
     * @return string url_long 原始长链接
     * @return int type 链接的类型，0：普通网页、1：视频、2：音乐、3：活动、5、投票
     * @return mixed
     */
    public function shorturl() {
        $api = new Domain_ServiceBaiduApi();

        $data = $api->shortUrl($this->url);
        if (count($data) == 1) {
            return $data[0];
        }
        return $data;
    }

    /**
     * 天气查询
     * @desc 参考http://www.heweather.com/documents/api
     * @return object aqi 空气质量
     * @return object basic 基本信息
     * @return object daily_forecast 7天天气预报
     * @return object now 实况天气
     * @return array hourly_forecast 每三小时天气预报
     * @return date hourly_forecast.date 时间
     * @return string hourly_forecast.hum 相对湿度（%）
     * @return string hourly_forecast.pop 降水概率
     * @return string hourly_forecast.pres 气压
     * @return string hourly_forecast.tmp 温度
     * @return object hourly_forecast.wind 风力风向
     * @return string hourly_forecast.wind.deg 风向（360度
     * @return string hourly_forecast.wind.dir 风向
     * @return string hourly_forecast.wind.sc 风力
     * @return string hourly_forecast.wind.spd 风速（kmph）
     *
     * @return object suggestion 生活指数，仅限国内城市
     * @return object suggestion.comf 舒适度指数
     * @return object suggestion.cw 洗车指数
     * @return object suggestion.drsg 穿衣指数
     * @return object suggestion.drsg 穿衣指数
     * @return object suggestion.flu 感冒指数
     * @return object suggestion.sport 运动指数
     * @return object suggestion.trav 旅游指数
     * @return object suggestion.uv 紫外线指数
     * @return mixed
     */

    public function weather() {
        $api = new Domain_ServiceBaiduApi();

        $data = $api->weather($this->city);
        if (count($data) == 1) {
            return $data[0];
        }
        return $data;
    }

    /**
     * 身份证号码查询
     * @desc
     * @return string address 所属地址
     * @return string sex 性别:M（男）F(女)
     * @return string birthday 生日
     */
    public function id() {
        $api = new Domain_ServiceBaiduApi();
        return $api->idservice($this->id);
    }

    /**
     * ip地址归属地查询
     * @desc
     * @return string ip IP地址
     * @return string country 国家
     * @return string province 省份，国外的默认值为none
     * @return string city 城市，国外的默认值为none
     * @return string district 地区，国外的默认值为none
     * @return string carrier 运营商
     */
    public function ip() {
        $api = new Domain_ServiceBaiduApi();
        return $api->IPService($this->ip);
    }

    /**
     * pm2.5查询
     * @desc pm2.5查询
     * @return string city 城市
     * @return date time 数据采集时间
     * @return int aqi 空气质量指数
     * @return string level 空气等级
     * @return string core 首要污染物
     */
    public function aqi() {
        $api = new Domain_ServiceBaiduApi();
        $data = $api->aqiService($this->city);
        $data['time'] = date("Y-m-d H:i:s", strtotime($data['time']));
        return $data;
    }

    /**
     * 手机号码归属地查询
     * @desc 支持批量查询
     * @return string areacode 区号
     * @return string province 省份
     * @return string city 城市
     * @return string operator 运营商
     * @return string phone 电话号码
     * @return string postcode 邮编
     */

    public function phone() {
        $api = new Domain_ServiceBaiduApi();
        return $api->phoneService($this->phone);
    }

    /**
     * 历史上的今天
     * @desc
     * @title 事件标题
     * @year 事件发生年份
     * @month 事件发生月份
     * @day 事件发生日期
     * @return mixed
     */
    public function historyToday() {
        $api = new Domain_ServiceBaiduApi();
        return $api->historyToday();
    }

    /**
     * 历史上的明天
     * @desc
     * @title 事件标题
     * @year 事件发生年份
     * @month 事件发生月份
     * @day 事件发生日期
     * @return mixed
     */
    public function historyTomorrow() {
        $api = new Domain_ServiceBaiduApi();
        return $api->historyTomorrow();
    }

    /**
     * 历史事件
     * @desc
     * @title 事件标题
     * @year 事件发生年份
     * @month 事件发生月份
     * @day 事件发生日期
     * @return mixed
     */
    public function historyDay() {
        $api = new Domain_ServiceBaiduApi();

        return $api->historyDay($this->month, $this->day, $this->num);
    }

    /**
     * 文字机器人服务
     * @desc
     * @return object data 参考http://www.tuling123.com/html/doc/api.html#backshuju
     */
    public function robot() {
        $api = new Domain_ServiceBaiduApi();
        if($this->info==""){
            throw new PhalApi_Exception("参数为空",400);
      }
        return $api->robot($this->info);
    }

    /**
     * 发送邮件
     * @desc
     * @return boolean isSend 发送邮件状态
     */
    public function sendMail() {
        return [
            'isSend' => DI()->mailer->send($this->receiver, $this->title, $this->content)
        ];
    }
}
